home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / (Sources) / Standard Controls / Buttons / XGStdButton.cpp next >
Text File  |  1997-04-24  |  8KB  |  400 lines

  1. /*    XGStdButton.cpp
  2.  *
  3.  *        This puts up a standard push button. This uses the standard
  4.  *    OS push button if it is available
  5.  */
  6.  
  7. /*  YAAF - Yet another application framework
  8.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  9.  *  
  10.  *  This library is free software; you can redistribute it
  11.  *  and/or modify it under the terms of the GNU Library
  12.  *  General Public License as published by the Free Software
  13.  *  Foundation; either version 2 of the License, or any
  14.  *  later version.
  15.  *  
  16.  *  This library is distributed in the hope that it will be
  17.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  18.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  19.  *  PURPOSE. See the GNU Library General Public License for
  20.  *  more details.
  21.  *  
  22.  *  You should have received a copy of the GNU Library General
  23.  *  Public License along with this library; if not, write to the
  24.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25.  *  Boston, MA 02111-1307, USA.
  26.  *  
  27.  *  To contact the author, either e-mail me at
  28.  *  woody@alumni.caltech.edu, or write to us at
  29.  *  
  30.  *          William Edward Woody
  31.  *          In Phase Consulting
  32.  *          1545 Ard Eevin Avenue
  33.  *          Glendale, CA 91202
  34.  */
  35.  
  36. #include <XDataUtil.h>
  37. #include <XStdButtons.h>
  38. #include <XWindow.h>
  39. #include <XError.h>
  40.  
  41. /************************************************************************/
  42. /*                                                                        */
  43. /*    Construction/Destruction                                            */
  44. /*                                                                        */
  45. /************************************************************************/
  46.  
  47. /*    XGStdButton::XGStdButton
  48.  *
  49.  *        create a push button
  50.  */
  51.  
  52. XGStdButton::XGStdButton(XGView *view, XGArgStream &stream, bool f) : 
  53.     XGView(view,stream,f)
  54. {
  55. #if OPT_MACOS == 1
  56.     fControl = NULL;
  57. #endif
  58. }
  59.  
  60. XGStdButton::XGStdButton(XGView *view, XGSViewInitRecord &i, bool f) : 
  61.     XGView(view,i,f)
  62. {
  63. #if OPT_MACOS == 1
  64.     fControl = NULL;
  65. #endif
  66. }
  67.  
  68. /*    XGStdButton::~XGStdButton
  69.  *
  70.  *        Delete me
  71.  */
  72.  
  73. XGStdButton::~XGStdButton()
  74. {
  75. #if OPT_MACOS == 1
  76.     if (fControl) ::DisposeControl(fControl);
  77. #endif
  78. }
  79.  
  80. /************************************************************************/
  81. /*                                                                        */
  82. /*    Control management                                                    */
  83. /*                                                                        */
  84. /************************************************************************/
  85.  
  86. /*    XGStdButton::DoDrawView
  87.  *
  88.  *        Draw my control. This draws the control in a wierd way.
  89.  */
  90.  
  91. void XGStdButton::DoDrawView(Rect)
  92. {
  93. #if OPT_MACOS == 1
  94.     XGDraw draw(this,false);
  95.     ::TextFont(fFont);
  96.     ::TextSize(fSize);
  97.     
  98.     if (fControl) ::Draw1Control(fControl);
  99. #endif
  100. }
  101.  
  102. /*    XGStdButton::DoActivate
  103.  *
  104.  *        Handle activation/deactivation
  105.  */
  106.  
  107. void XGStdButton::DoActivate(bool active)
  108. {
  109. #if OPT_MACOS == 1
  110.     XGDraw draw(this,false);
  111.     ::TextFont(fFont);
  112.     ::TextSize(fSize);
  113.     
  114.     if (fControl && IsEnabled()) {
  115.         if (active) ::HiliteControl(fControl,0);
  116.         else ::HiliteControl(fControl,255);
  117.     }
  118. #endif
  119.  
  120. #if OPT_WINOS == 1
  121.     #pragma unused(active)
  122. #endif
  123. }
  124.  
  125. /*    XGStdButton::DoMouseDown
  126.  *
  127.  *        Handle mouse click
  128.  */
  129.  
  130. bool XGStdButton::DoMouseDown(Point pt, short)
  131. {
  132. #if OPT_MACOS == 1
  133.     short i;
  134.     
  135.     if (!fControl) return false;
  136.     {
  137.         XGDraw draw(this,false);
  138.         ::TextFont(fFont);
  139.         ::TextSize(fSize);
  140.         ViewToGlobal(&pt);
  141.  
  142.         i = ::TrackControl(fControl,pt,NULL);
  143.     }
  144.     if (i) ReceiveDispatch(KEventButton,GetViewID(),(void *)this);
  145. #endif
  146.  
  147.  
  148. #if OPT_WINOS == 1
  149.     #pragma unused(pt)
  150. #endif
  151.  
  152.     return false;
  153. }
  154.  
  155. /*    XGStdButton::DoMoveView
  156.  *
  157.  *        Move this view to the new location
  158.  */
  159.  
  160. void XGStdButton::DoMoveView(void)
  161. {
  162. #if OPT_MACOS == 1
  163.     XGDraw draw(this,false);
  164.     Rect r;
  165.     ::TextFont(fFont);
  166.     ::TextSize(fSize);
  167.     
  168.     r = GetContentRect();
  169.     ViewToGlobal(&r);
  170.     if (fControl) ::MoveControl(fControl,r.left,r.top);
  171. #endif
  172. }
  173.  
  174. /*    XGStdButton::DoSizeView
  175.  *
  176.  *        This handles the resize event
  177.  */
  178.  
  179. void XGStdButton::DoSizeView(void)
  180. {
  181. #if OPT_MACOS == 1
  182.     XGDraw draw(this,false);
  183.     Rect r;
  184.     ::TextFont(fFont);
  185.     ::TextSize(fSize);
  186.     
  187.     r = GetContentRect();
  188.     ViewToGlobal(&r);
  189.     if (fControl) ::SizeControl(fControl,r.right-r.left,r.bottom-r.top);
  190. #endif
  191. }
  192.  
  193. /*    XGStdButton::ShowView
  194.  *
  195.  *        This also shows the control
  196.  */
  197.  
  198. void XGStdButton::ShowView()
  199. {
  200.     XGView::ShowView();
  201.  
  202. #if OPT_MACOS == 1
  203.     if (fControl) {
  204.         XGDraw draw(this,false);
  205.         ::TextFont(fFont);
  206.         ::TextSize(fSize);
  207.  
  208.         ::ShowControl(fControl);
  209.     }
  210. #endif
  211. }
  212.  
  213. /*    XGStdButton::HideView
  214.  *
  215.  *        This also hides the control
  216.  */
  217.  
  218. void XGStdButton::HideView()
  219. {
  220.     XGView::HideView();
  221. #if OPT_MACOS == 1
  222.     if (fControl) {
  223.         XGDraw draw(this,false);
  224.         ::TextFont(fFont);
  225.         ::TextSize(fSize);
  226.  
  227.         ::HideControl(fControl);
  228.     }
  229. #endif
  230. }
  231.  
  232. /*    XGStdButton::EnableView
  233.  *
  234.  *        This also enables the control.
  235.  */
  236.  
  237. void XGStdButton::EnableView()
  238. {
  239.     XGView::EnableView();
  240. #if OPT_MACOS == 1
  241.     if (fControl && IsActive()) {
  242.         XGDraw draw(this,false);
  243.         ::TextFont(fFont);
  244.         ::TextSize(fSize);
  245.  
  246.         ::HiliteControl(fControl,255);
  247.     }
  248. #endif
  249. }
  250.  
  251. /*    XGStdButton::DisableView
  252.  *
  253.  *        This also disables the control
  254.  */
  255.  
  256. void XGStdButton::DisableView()
  257. {
  258.     XGView::DisableView();
  259. #if OPT_MACOS == 1
  260.     if (fControl && IsActive()) {
  261.         XGDraw draw(this,false);
  262.         ::TextFont(fFont);
  263.         ::TextSize(fSize);
  264.  
  265.         ::HiliteControl(fControl,0);
  266.     }
  267. #endif
  268. }
  269.  
  270. /************************************************************************/
  271. /*                                                                        */
  272. /*    Message Dispatch                                                    */
  273. /*                                                                        */
  274. /************************************************************************/
  275.  
  276. /*    XGStdButton::ReceiveDispatch
  277.  *
  278.  *        Receive messages. Pass them upwards.
  279.  *
  280.  *        On Windows, this interprets the internal message that is
  281.  *    generated when a control is clicked and translates it into my
  282.  *    more general purpose control messages
  283.  */
  284.  
  285. long XGStdButton::ReceiveDispatch(long msg, long arg, void *parg)
  286. {
  287. #if OPT_WINOS == 1
  288.     XGSWInternalRecord *w;
  289.     
  290.     if (msg == KEventWInternal) {
  291.         /*
  292.          *    Process the BN_CLICKED command message, turning this
  293.          *    into a KEventButton message.
  294.          */
  295.         
  296.         w = (XGSWInternalRecord *)parg;
  297.         if (w->msg == WM_COMMAND) {
  298.             short notify;
  299.             
  300.             notify = HIWORD(w->wp);
  301.             if (notify == BN_CLICKED) {
  302.                 ReceiveDispatch(KEventButton,GetViewID(),(void *)this);
  303.                 return 0;
  304.             }
  305.         }
  306.         return 1;
  307.     }
  308. #endif
  309.  
  310.     return XGView::ReceiveDispatch(msg,arg,parg);
  311. }
  312.  
  313. /************************************************************************/
  314. /*                                                                        */
  315. /*    Button Values                                                        */
  316. /*                                                                        */
  317. /************************************************************************/
  318.  
  319. /*    XGStdButton:GetValue
  320.  *
  321.  *        Get value
  322.  */
  323.  
  324. short XGStdButton::GetValue(void)
  325. {
  326. #if OPT_MACOS == 1
  327.     return ::GetControlValue(fControl);
  328. #endif
  329.  
  330. #if OPT_WINOS == 1
  331.     int i;
  332.     
  333.     i = ::SendMessage(_GetHWND(),BM_GETCHECK,0,0);
  334.     if (i == BST_CHECKED) return 1;
  335.     if (i == BST_UNCHECKED) return 0;
  336.     return 2;            /* Indeterminate (tri-state) */
  337. #endif
  338. }
  339.  
  340. /*    XGStdButton::SetValue
  341.  *
  342.  *        Set the value of this thing
  343.  */
  344.  
  345. void XGStdButton::SetValue(short x)
  346. {
  347. #if OPT_MACOS == 1
  348.     XGDraw draw(this,false);        // make sure set up right
  349.     ::TextFont(fFont);
  350.     ::TextSize(fSize);
  351.  
  352.     ::SetControlValue(fControl,x);
  353. #endif
  354.  
  355. #if OPT_WINOS == 1
  356.     if (x < 0) x = 0;
  357.     if (x > 2) x = 0;
  358.     ::SendMessage(_GetHWND(),BM_SETCHECK,(WPARAM)x,0);
  359. #endif
  360. }
  361.  
  362. /*    XGStdButton::GetText
  363.  *
  364.  *        Get the text contents
  365.  */
  366.  
  367. void XGStdButton::GetText(char *str)
  368. {
  369. #if OPT_MACOS == 1
  370.     ::GetControlTitle(fControl,(unsigned char *)str);
  371.     p2cstr((unsigned char *)str);
  372. #endif
  373.  
  374. #if OPT_WINOS == 1
  375.     ::GetWindowText(_GetHWND(),str,256);        // assume 256 byte buffer
  376. #endif
  377. }
  378.  
  379. /*    XGStdButton::SetText
  380.  *
  381.  *        Set the text contents of this button
  382.  */
  383.  
  384. void XGStdButton::SetText(char *str)
  385. {
  386. #if OPT_MACOS == 1
  387.     XGDraw draw(this,false);
  388.     unsigned char buffer[256];
  389.     ::TextFont(fFont);
  390.     ::TextSize(fSize);
  391.     
  392.     cvtc2p(buffer,str);
  393.     ::SetControlTitle(fControl,buffer);
  394. #endif
  395.  
  396. #if OPT_WINOS == 1
  397.     ::SetWindowText(_GetHWND(),str);
  398. #endif
  399. }
  400.